package com.student.dao.impl;

import java.util.List;

import org.springframework.stereotype.Component;

import com.student.dao.StudentQueryDao;
import com.student.model.Area;
import com.student.model.Colleges;
import com.student.model.Profession;
import com.student.model.Student;
import com.student.model.Subjects;
import com.student.util.PageInfo;

@Component("studentQueryDao")
public class StudentQueryDaoImpl extends HibernateDaoImpl implements StudentQueryDao {

	public List<Student> getAllStudent(Long collegeId, Long professionId,
			Integer clazz, Long subjectsId, Long areaId, PageInfo pageInfo) throws Exception {
		String hql = "FROM Student s where 1=1";
		if(collegeId != null) {
			hql += " AND s.profession.colleges.collegeId='" + collegeId + "'";
		}
		if(professionId != null) {
			hql += " AND s.profession.professionId='" + professionId + "'";
		}
		if(clazz != null) {
			hql += " AND s.clazz='" + clazz + "'";
		}
		if(subjectsId != null) {
			hql += " AND s.subjects.subjectsId='" + subjectsId + "'";
		}
		if(areaId != null) {
			hql += " AND s.area.areaId='" + areaId + "'";
		}
		hql+=" order by s.profession.colleges.collegeId";
		
		System.out.println(hql);
		if(pageInfo == null) {
			return this.find(hql);
		}
		return this.findPageByQuery(hql, null, pageInfo);
	}

	public List<Colleges> getAllCollege() throws Exception {
		List<Colleges> colleges = this.find("FROM Colleges");
		return colleges;
	}

	public List<Profession> getAllProfession(Long collegeId) throws Exception {
		String hql = "From Profession p where p.colleges.collegeId='" + collegeId + "'";
		return this.find(hql);
	}

	public List<Area> getAllArea() throws Exception {
		String hql = "FROM Area";
		List<Area> list = this.find(hql);
		return list;
	}

	public List<Subjects> getAllSubject() throws Exception {
		String hql = "FROM Subjects";
		List<Subjects> list = this.find(hql);
		return list;
	}
	
	public Profession getProfessionById(Long professionId) throws Exception {
		String hql = "FROM Profession p where p.professionId='" + professionId + "'";
		return (Profession) this.find(hql).get(0);
	}

	public List<Student> getAllStudent(String examCode, String idCode,
			String studentName) throws Exception {
		String hql = "FROM Student s where 1=1";
		if(examCode != null) {
			hql += " AND s.examcode like '%" + examCode + "'";
		}
		if(idCode != null) {
			hql += " AND s.idCode like '%" + idCode + "'";
		}
		if(studentName != null) {
			hql += " AND s.studentName='" + studentName + "'";
		}
		
		return this.find(hql);
	}

	public List<Student> getAllStudent(Long collegeId, Long professionId,
			Integer clazz, Long subjectsId, Long areaId,
			String paymentOperation, Double unPaidAmount, PageInfo pageInfo)
			throws Exception {
		String hql = "FROM Student s where 1=1";
		if(collegeId != null) {
			hql += " AND s.profession.colleges.collegeId='" + collegeId + "'";
		}
		if(professionId != null) {
			hql += " AND s.profession.professionId='" + professionId + "'";
		}
		if(clazz != null) {
			hql += " AND s.clazz='" + clazz + "'";
		}
		if(subjectsId != null) {
			hql += " AND s.subjects.subjectsId='" + subjectsId + "'";
		}
		if(areaId != null) {
			hql += " AND s.area.areaId='" + areaId + "'";
		}
		if(paymentOperation != null && !"".equals(paymentOperation)) {
			if(unPaidAmount != null) {
				hql += " AND s.payment.unpaidFees" + paymentOperation + "'" + unPaidAmount + "'";
			}
		}
		hql+=" order by s.profession.professionId,s.clazz";
		System.out.println(hql);
		if(pageInfo == null) {
			return this.find(hql);
		}
		return this.findPageByQuery(hql, null, pageInfo);
	}

}
